// pages/reusername/reusername.js

import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
var config = require('../../config.js')

Page({

  /**
   * 页面的初始数据
   */

  data: {
      phone:'',
      uuid:"",
      user_new_add_show:false,
      user_sel_show:false,
      title_add_text:"新增",
      userlist:[],
     
      user_columns: ['默认用户', '指纹用户', '密码用户', 'IC卡用户', '钥匙用户','手机用户','人脸用户','掌纹用户','虹膜用户'],
 
    user_option: [
      { text: '默认用户', value: 0 },
      { text: '指纹用户', value: 1 },
      { text: '密码用户', value: 2 },
      { text: 'IC卡用户', value: 3 },
      { text: '钥匙用户', value: 4 },
      { text: '手机用户', value: 5 },
      { text: '人脸用户', value: 6 },
      { text: '掌纹用户', value: 7 },
      { text: '虹膜用户', value: 8 },
      { text: '临时用户', value: 9 },
    ],

   

    // option1: [
    //   { text: '全部商品', value: 0 },
    //   { text: '新款商品', value: 1 },
    //   { text: '活动商品', value: 2 },
    // ],
    // value1: 0,
 
    user_value: 0,

    oldusertype: '默认用户',

    usertype: '默认用户',
    username:"",
    userid:0,
  },
  

  user_onChange(event) {
    const { picker, value, index } = event.detail;
    // Toast(`当前值：${value}, 当前索引：${index}`);
    console.log("", event.detail);

    this.setData({
      usertype: event.detail.value,
    })
    // user_sel_show: false
  },
  // onUserTypeChange(event) {
  //   console.log("onUserTypeChange:", event.detail);
  // },
  onUserIdChange(event) {
    // console.log("onUserIdChange:", event.detail);
    this.setData({
      userid: event.detail
    })
  },
  onUserNameChange(event) {
    // console.log("onUserNameChange:", event.detail);
    this.setData({
      username: event.detail
    })
  },

  onAddreUser()
  {
    console.log('user_id', this.data.userid)
    console.log('rename', this.data.username)
    console.log('user_type', this.data.usertype)

    if (this.data.username == '')
    {
      wx.showToast({
        title: '名称不能为空',
        icon: 'none',
        duration: 2000
      })
    }
    else
    {
      this.addReUser(this.data.userid, this.data.username, this.data.usertype)
    }
   
  },

  onDelFun(val){
    console.log('val',val)
   // wx.showToast({ title: '删除', icon: 'none' });
    Dialog.confirm({
      title: '提示',
      message: '确认删除该用户备注？',
    })
      .then(() => {
        // on confirm
        this.delReUser(val.currentTarget.dataset.value)
      })
      .catch(() => {
        // on cancel
       
      });
   
  }
,
  onClickLeft() {
   // wx.showToast({ title: '点击返回', icon: 'none' });

    if (!this.data.user_new_add_show)
    {
      wx.navigateBack({
        delta: 1
      })
    }
    else
    {
      this.setData({
        user_new_add_show: false,
        user_sel_show: false,
        title_add_text: "新增",
      })
    }
   
  },
  onClickRight() {
   // wx.showToast({ title: '点击按钮' + this.data.user_new_add_show, icon: 'none' });
    // 新增用户测试start
    wx.navigateTo({
      url: '../addUser/addUser?uuid=' + this.data.uuid + '&phone=' + this.data.phone,
    })
    return
    // 新增用户测试end

    if (!this.data.user_new_add_show)
    {
     
        this.setData({
          user_new_add_show: !this.data.user_new_add_show,
          title_add_text: "",
        })
    }
    else
    {
        this.setData({
          user_new_add_show: !this.data.user_new_add_show,
          title_add_text: "新增",
        })
      
    }
   

  },

  user_onCancel(event)
  {
    console.log("", event.detail);
    this.setData({
      usertype: this.data.oldusertype,
      user_sel_show: false
    })
  },

  user_onConfirm(event) {
    console.log("", event.detail);
    this.setData({
      usertype: event.detail.value,
      oldusertype: event.detail.value,
      user_sel_show: false
    })
  },

  onclik_user_list()
  {
    console.log("", this.data.user_sel_show)

    this.setData({
      user_sel_show: !this.data.user_sel_show
    })
  },

  addReUser(user_id, rename, user_type) {
    var _this = this
    wx.request({
      url: config.url.addReUser,
      method: 'POST',
      dataType: "json",
      data: {
        uuid: this.data.uuid,
        user_id: user_id,
        rename: rename,
        user_type: user_type,
        client: config.client
      },
      header: {
        // 'content-type': 'application/json', // 默认值
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      success(res) {
        console.log(res.data)
        console.log("code:", res.data.code)
        console.log("length:", res.data.data.length)

        
        if (res.data.code == 200) {
          _this.setData({
            user_new_add_show: false,
            user_sel_show: false,
            title_add_text: "新增",
          })
          _this.getReUserList()
        }
        else {
          wx.showToast({
            title: '添加失败',
            icon: 'none',
            duration: 2000
          })

        }

      },
      fail(e) {
        console.error(e)

      }
    })

  },

  getReUserList()
  {
      var _this = this
      wx.request({
        url: config.url.getReUser,
        method: 'POST',
        dataType: "json",
        data: {
          uuid: this.data.uuid,
          'client': config.client
        },
        header: {
          // 'content-type': 'application/json', // 默认值
          "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        success(res) {
          console.log(res.data)
          console.log("code:", res.data.code)
          console.log("length:", res.data.data.length)

          if (res.data.code == 200) {
            if (res.data.data.length == 0) 
            {
              _this.setData({
                userlist: []
              })
              wx.showToast({
                title: '没有获取到备注信息',
                icon: 'none',
                duration: 2000
              })

            }
            else
            {
              _this.setData({
                userlist: res.data.data
              })
            }
           

          
          }
          else {
            wx.showToast({
              title: '没有获取到用户',
              icon: 'none',
              duration: 2000
            })

          }

        },
        fail(e) {
          console.error(e)

        }
      })
    
  },

  delReUser(user_id) {
    var _this = this
    wx.request({
      url: config.url.delReUser,
      method: 'POST',
      dataType: "json",
      data: {
        uuid: this.data.uuid,
        user_id: user_id,
        'client': config.client
      },
      header: {
        // 'content-type': 'application/json', // 默认值
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      success(res) {
        console.log(res.data)
        console.log("code:", res.data.code)
        console.log("length:", res.data.data.length)

        if (res.data.code == 200) {
          _this.getReUserList()
        }
        else {
          wx.showToast({
            title: '删除失败',
            icon: 'none',
            duration: 2000
          })

        }

      },
      fail(e) {
        console.error(e)

      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    console.log("options:", options)
    console.log("uuid:", options.uuid)
    console.log("phone:", options.phone)
    this.setData({
      uuid: options.uuid,
      phone: options.phone
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getReUserList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})